package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

@Autowired
RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomAttrValueService attrValueService;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
@Autowired
BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {

        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        RoomInfo roomInfo = getById(id);
        if (roomInfo == null) {
            return null;

        }
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartVo(roomInfo.getApartmentId());
        List<GraphVo> graphVo=graphInfoService.getGraphVo(id);
        List<AttrValueVo> attrValueVoList=attrValueService.getAttrValueVo(id);
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByRoomId(id);
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByRoomId(id);
        List<PaymentType> paymentTypeList=roomPaymentTypeService.selectListByRoomId(id);
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectListByRoomId(roomInfo.getApartmentId());
        List<LeaseTerm> leaseTermList= leaseTermMapper.selectListByRoomId(id);
        Long singout = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().
                eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setIsCheckIn(singout > 0);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setGraphVoList(graphVo);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        browsingHistoryService.saveHistory(LoginUserContext.getUser().getUserId(), id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
       return roomInfoMapper.pageItemByApartmentId(page, id);

    }
}




